<?php

namespace Addons\Zmphoto\Controller;

use Home\Controller\AddonsController;

class WapController extends AddonsController {

   public function _initialize(){
       get_openid('ofCONv1WVGN2zjF-lOv-rF_nuTc8');
   }

	//选择活动
	public function index(){
		/*获取对应token的活动*/
		$data =D('Addons://Zmphoto/PhotoZmActivity')->getActList();
		$this->assign('data',$data);
		$this->display();
	}

    public function studInfo(){
        $aid = I('aid',0,'intval');
        $data = $this->getActInfo($aid);
        /*获取用户信息并判断用户是否是首次登录*/
        $uinfo = $this->userInfoByopenId($aid);
        $this->assign('info',$uinfo);
        $this->display();
    }



    public function first(){
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);
        /*已经绑定，跳回活动页*/
        $where['aid'] = $data['id'];
        $where['openid'] = get_openid();
        if($s = D('Addons://Zmphoto/PhotoZmData')->getOne($where))  $this->redirect('studInfo',array('id'=>$data['id'])); 
        unset($where);

        if(IS_POST){
            $model = D('Addons://Zmphoto/PhotoZmData');  
            if($info = $model->create()){
                $where['aid'] = $data['id'];
                $where['card_id'] = $info['card_id'];
                if(! $s = $model->getOne($where)) $this->error('身份证号码输入错误!');
                if($s['openid']) $this->error('当前身份证已经绑定用户');
                if($model->update($where,array('openid'=>get_openid(),'update_time'=>time(),'mobile'=>$info['mobile']))){
                    $this->success('绑定成功！',addons_url('studInfo',array('id'=>$data['id'])));
                }else{
                    $this->error('请重新绑定!');
                }
            }else{
                $this->error($model->getError());
            }
        }else{
            $this->display();
        }
        
    }

    /*修改学生信息*/
    public function updateInfo(){
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);
        $udata = $this->userInfoByopenId($aid);
        if(IS_POST){
           $model = D('Addons://Zmphoto/PhotoZmData');  
           $mobile = I('post.mobile');
           if(!$mobile) $this->error('请输入手机号码');
           if($model->update($udata['id'],array('mobile'=>$mobile))){
                $this->success('修改成功!',U('studInfo',array('aid'=>$aid)));
           }else{
                $this->error('请重新确定');
           }
        }else{
            $this->assign('isupdate',1);
            $this->display('first');
        }
    }

    /*意见反馈*/
    public function infoReview(){
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);
        $udata = $this->userInfoByopenId($aid);
        if(IS_POST){
            $datas['content'] = I('post.content');
            $datas['uid'] = $udata['id'];
            $datas['aid'] = $aid;
            $datas['token'] = get_token();
            $datas['create_time'] = time();
            $model = D('Addons://Zmphoto/PhotoZmReview'); 
            if($model->addData($datas)){
                $this->success('意见反馈成功，我们会在五个工作日内回复您',U('studInfo',array('aid'=>$aid)));
            }else{
                $this->error('请重新确定');
            }
        }else{
             $data = getUserInfo(get_uid_by_openid());
             $this->assign('data',$data);
             $this->display();
        }
    }  

    /*学生二维码*/
    public function studCode(){
        phpinfo();
        exit;
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);
        $udata= $this->userInfoByopenId($aid,'id,username,no'); //获取用户信息
        $this->assign('udata',$udata);  
        $save_path = isset($_GET['save_path'])?$_GET['save_path']:'qrcode/';  //图片存储的绝对路径
        $web_path = isset($_GET['save_path'])?$_GET['web_path']:'qrcode/';        //图片在网页上显示的路径
        $qr_data = isset($_GET['qr_data'])?$_GET['qr_data']:U('studPhoto',array('aid'=>$aid,'uid'=>$udata['id']));
        $qr_level = isset($_GET['qr_level'])?$_GET['qr_level']:'H';
        $qr_size = isset($_GET['qr_size'])?$_GET['qr_size']:'10';
        $save_prefix = isset($_GET['save_prefix'])?$_GET['save_prefix']:'ZETA';
        if($filename = $this->createQRcode($save_path,$qr_data,$qr_level,$qr_size,$save_prefix)){
            $pic = $web_path.$filename;
        }
        $this->assign('pic',$pic);
        $this->display();
    }


    /*摄影师注册*/
        public function photoRe(){
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);
        /*已经绑定，跳回活动页*/
        $where['aid'] = $data['id'];
        $where['openid'] = get_openid();
        $s = D('Addons://Zmphoto/PhotoZmGl')->getOne($where);
        if($s && $s['status']){
            $this->success('你已成功注册!',U('photoIndex',array('aid'=>$aid)));   //成功注册 
            exit;
        }
        if(IS_POST){
            $model = D('Addons://Zmphoto/PhotoZmGl');  
            if($info = $model->create()){
                $info['openid'] = get_openid();
                $info['token'] = get_token();
                $info['aid'] = $id;
                if($model->add($info)){
                    $this->success('注册成功!请等待后台审核',U('index'));
                }else{
                    $this->error('请重新注册!');
                }
            }else{
                $this->error($model->getError());
            }
            
        }else{
            $this->display();
        }
        
    }


    /*摄影师登录*/
    public function photoIndex(){
        $aid = I('get.aid',0,'intval');
        $data = $this->getActInfo($aid);    
        $udata = $this->getGlInfo($aid);    //获取摄影师信息
        $this->display();   
    }


    /*摄影师扫描*/
    public function studPhoto(){
        $aid = I('get.aid',0,'intval');
        $uid = I('get.uid',0,'intval');
        if(IS_POST){
            $img = I('post.img');
            $data['photo_gl'] =  D('Addons://Zmphoto/PhotoZmGl')->getFie(array('openid'=>get_openid()),'thumb');   //获取字段值
            $data['photo_url'] =  down_file_media($img,'image');  //下载图片
            $model = D('Addons://Zmphoto/PhotoZmData');
            if($model->update(array('id'=>$uid),$data)){
                $this->success('成功!');
            }else{
                $this->error('失败');
            }
        }else{
            $data  = $this->getActInfo($aid);
            $udata = $this->userInfo($uid);  //获取用户信息
            $gdata = $this->getGlInfo($data['id']);  //获取管理员信息
            $this->assign('udata',$udata);
            $this->display(); 
        }
        
    }

    /*用户信息确认*/
    public function photoOk(){
        $aid = I('get.aid',0,'intval');
        $uid = I('get.uid',0,'intval');
        $openid = get_openid();
        $data  = $this->getActInfo($aid);
        $udata = $this->userInfo($uid);  //获取用户信息
        if($udata['openid'] != $openid) $this->error('当前用户信息错误!');

        $s['confirm'] = 1;
        $s['confirm_time'] = time();
        $model = D('Addons://Zmphoto/PhotoZmData');
        if($model->update(array('id'=>$uid),$s)){
            $this->success('成功!');
        }else{
            $this->error('失败');
        }
    }





    /*获取活动信息*/
    public function getActInfo($id){
        if(!$id || !$data = D('Addons://Zmphoto/PhotoZmActivity')->getOne(array('id'=>$id)))   $this->error('活动不存在!');
        $now = time();
        if($now<$data['start_time']) $this->error('活动暂未开始');
        if($now>$data['end_time'])   $this->error('活动已经结束');

        return $data;
    }

    /*通过openid获取学生信息*/
    public function userInfoByopenId($aid,$field=''){
        $where['openid'] = get_openid();
        $where['aid'] = $aid;
        /*没值则为首次登录，跳转到绑定页面*/
        $data = D('Addons://Zmphoto/PhotoZmData')->getOne($where,$field);
        if(!$data){
            $this->redirect('first',array('aid'=>$aid));
        }
        return $data;
    }


    /*获取用户信息*/
    public function userInfo($id){
        if(!$id || !$data = D('Addons://Zmphoto/PhotoZmData')->getOne(array('id'=>$id)))   $this->error('用户不存在!');
        return $data;
    }

    /*判断当前摄影师身份*/
    public function getGlInfo($aid){
        $openid = get_openid();
        if(!$aid || !$data = D('Addons://Zmphoto/PhotoZmGl')->getOne(array('aid'=>$aid,'openid'=>get_openid())))   $this->error('摄影师不存在!');
        return $data;
    }


    /*生成二维码*/
    public function createQRcode($save_path,$qr_data='PHP QR Code :)',$qr_level='L',$qr_size=4,$save_prefix='qrcode'){
        if(!isset($save_path)) return '';
        //设置生成png图片的路径
        $PNG_TEMP_DIR = & $save_path;
        //导入二维码核心程序
       vendor('phpqrcode.phpqrcode');
        //检测并创建生成文件夹
        if (!file_exists($PNG_TEMP_DIR)){
            mkdir($PNG_TEMP_DIR);
        }
        $filename = $PNG_TEMP_DIR.'test.png';
        $errorCorrectionLevel = 'L';
        if (isset($qr_level) && in_array($qr_level, array('L','M','Q','H'))){
            $errorCorrectionLevel = & $qr_level;
        }
        $matrixPointSize = 4;
        if (isset($qr_size)){
            $matrixPointSize = & min(max((int)$qr_size, 1), 10);
        }
        if (isset($qr_data)) {
            if (trim($qr_data) == ''){
                die('data cannot be empty!');
            }

            //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
            $filename = $PNG_TEMP_DIR.$save_prefix.md5($qr_data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
            //开始生成
           \QRcode::png($qr_data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
        } else {
            //默认生成
            \QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2,true);
        }
        if(file_exists($PNG_TEMP_DIR.basename($filename)))
            return basename($filename);
        else
            return FALSE;
    }

}